<template>
  <el-dialog
    :title="!dataForm.listid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="工单编号" prop="listno">
      <el-input v-model="dataForm.listno" placeholder="工单编号"></el-input>
    </el-form-item>
    <el-form-item label="计件项目" prop="listproject">
      <el-input v-model="dataForm.listproject" placeholder="计件项目"></el-input>
    </el-form-item>
    <el-form-item label="预计总工时" prop="pretime">
      <el-input v-model="dataForm.pretime" placeholder="预计总工时"></el-input>
    </el-form-item>
    <el-form-item label="总数量" prop="allnumber">
      <el-input v-model="dataForm.allnumber" placeholder="总数量"></el-input>
    </el-form-item>
    <el-form-item label="预计项目总金额" prop="premoney">
      <el-input v-model="dataForm.premoney" placeholder="预计项目总金额"></el-input>
    </el-form-item>
    <el-form-item label="计划完成日期" prop="prtime">
      <el-input v-model="dataForm.prtime" placeholder="计划完成日期"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="empname">
      <el-input v-model="dataForm.empname" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="工单状态" prop="liststate">
      <el-input v-model="dataForm.liststate" placeholder="工单状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          listid: 0,
          listno: '',
          listproject: '',
          pretime: '',
          allnumber: '',
          premoney: '',
          prtime: '',
          empname: '',
          liststate: ''
        },
        dataRule: {
          listno: [
            { required: true, message: '工单编号不能为空', trigger: 'blur' }
          ],
          listproject: [
            { required: true, message: '计件项目不能为空', trigger: 'blur' }
          ],
          pretime: [
            { required: true, message: '预计总工时不能为空', trigger: 'blur' }
          ],
          allnumber: [
            { required: true, message: '总数量不能为空', trigger: 'blur' }
          ],
          premoney: [
            { required: true, message: '预计项目总金额不能为空', trigger: 'blur' }
          ],
          prtime: [
            { required: true, message: '计划完成日期不能为空', trigger: 'blur' }
          ],
          empname: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          liststate: [
            { required: true, message: '工单状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.listid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.listid) {
            this.$http({
              url: this.$http.adornUrl(`/listinfo/listinfo/info/${this.dataForm.listid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.listno = data.listinfo.listno
                this.dataForm.listproject = data.listinfo.listproject
                this.dataForm.pretime = data.listinfo.pretime
                this.dataForm.allnumber = data.listinfo.allnumber
                this.dataForm.premoney = data.listinfo.premoney
                this.dataForm.prtime = data.listinfo.prtime
                this.dataForm.empname = data.listinfo.empname
                this.dataForm.liststate = data.listinfo.liststate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/listinfo/listinfo/${!this.dataForm.listid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'listid': this.dataForm.listid || undefined,
                'listno': this.dataForm.listno,
                'listproject': this.dataForm.listproject,
                'pretime': this.dataForm.pretime,
                'allnumber': this.dataForm.allnumber,
                'premoney': this.dataForm.premoney,
                'prtime': this.dataForm.prtime,
                'empname': this.dataForm.empname,
                'liststate': this.dataForm.liststate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
